March 6, 2019

Data types and display standard

  • Add color and faceting to explore more variable
  • Careful about over-plotting
  • If not necessary, avoid 3D plots, instead use contour
  • Common plots depending on data types
Data Type Common Plot Type
Numerical histogram, dotplot, density plot, scatterplot, boxplot
Categorical bar chart, mosaic
Date time series
Mixed boxplot, parallel cordinate plot
Spatial maps, google map, contour plot or 2d density

Drawing a map: basic idea

##   lat long gp ord
## 1   1    1 g1   1
## 2   3    2 g1   2
## 3   6    3 g2   1
## 4   2    4 g1   3
## 5   1    1 g1   4
## 6   6    3 g2   2
## 7   2    4 g2   3
## 8   3    2 g2   4
  • Notice: the order of the data maters
  • order is maintained within each group
library(ggplot2)
ggplot(map.dat, aes(lat,long)) +  
  geom_polygon(aes(fill=gp))

Map visualization

library(maps)
map.dat <- map_data("world")
ggplot() + geom_polygon(aes(long,lat, group=group), fill="grey65", data=map.dat) + 
  theme_bw() + theme(axis.text = element_blank(), axis.title=element_blank()) 

Colourful world

ggplot(map.dat, aes(x=long, y=lat, group=group, fill=region))+ geom_polygon() + 
  theme(legend.position = "none")

- Color should be used to display data

USA county map

us.dat <- map_data("state")
ct.dat <- map_data("county")
ggplot() + geom_polygon(aes(long,lat, group=group), fill="grey65", data=ct.dat) + 
  geom_polygon(aes(long,lat, group=group), color='white', fill=NA, data=us.dat) + 
  theme_bw() + theme(axis.text = element_blank(), axis.title=element_blank()) 

USA polyconic map

library(scales) # for function alpha()
ggplot(us.dat, aes(x=long, y=lat, group=group)) + 
  geom_polygon(fill="grey65", colour = alpha("white", 1/2), size = 0.2) + theme_bw() + 
  theme(legend.position = "none", text = element_blank(), line = element_blank()) + 
  coord_map("polyconic") 

Showing data on maps

pdat <- read.csv("./tempdat.csv")
ggplot(pdat, aes(x=long, y=lat,group=group)) + 
  geom_polygon(aes(fill=temp), colour = alpha("white", 1/2), size = 0.2) + theme_bw() + 
  theme(legend.position = "none", text = element_blank(), line = element_blank()) + 
  scale_fill_continuous(low="blue", high="hotpink") 

Showing data on maps

  • World petroleum use data, obtained from Nasa website

  • We need to clean the data before plotting such a map

  • Yet we see trouble with the data

Showing unemployment data on maps

ggplot(choropleth, aes(long, lat, group = group)) +
  geom_polygon(aes(fill = rate_d), colour = alpha("white", 1/2), size = 0.2) + 
  geom_polygon(data = state_df, colour = "white", fill = NA) +
  scale_fill_brewer(palette = "PuRd")

Geo-spatial data visualization

  • ggmap package of R works with ggplot
## install.packages("ggmap")
library(ggmap)

Google API in action

Omaha crimes map (spotcrime.com)

Map options

Plotting data as block on maps

Road map with zoom option

Where are the crimes?

Black and white map

Interactive maps

library(leaflet)
leaflet() %>%
  addTiles() %>%  # use the default base map which is OpenStreetMap tiles
  addMarkers(lat = 41.257876, lng = -96.012557,
             popup = "University of Nebraska Omaha")

Plotting omaha crimes

References and useful resources